import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { config } from '../../../shared/smartadmin.config';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入

declare var $: any;
declare var QRCode: any;
declare var html2canvas:any;
@Component({
	selector: 'sa-siteCode2',
	templateUrl: './siteCode2.component.html',
})
export class SiteCode2Component implements OnInit {

	tableData: any = []; //table数据
	totalCount: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //内置的分页每页条数
	searchKey: string = ''; //搜索keyword

	carNumber = ''; //车牌号码
	deviceId: string = ''; //设备ID
	companyName: string = ''; //公司名称
	companyId: string = '';
	searchVal:string='';
	startTime: string = ''; //开始时间
	endTime: string = ''; //结束时间
	
	downloadRow:any;
	downloadType:string = 'car';
	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage||1;
		});
	}
	//时间选中回调
	getSelectTableTime(time) {
		this.startTime = time.startTime;
		this.endTime = time.endTime;
	}
	//公司选中回调
	companySelected(data){
		this.companyId = data.companyId;
		this.companyName = data.companyName;
		this.deviceId = data.deviceId;
		this.carNumber = data.carNumber;
	}
	ngOnInit() {
		this.load()
	}

	//加载表格数据
	load() {
		var postData = {
			pageSize: this.pageSize,
			nowPage: this.curPage,
			parameters: {
				searchKey: this.searchVal,
			}
		}
		let reqdata = 'gridPager=' + JSON.stringify(postData);
		this.jsonApiService.http("QRcodeStationManage/QRcodeStationManage.html", reqdata).subscribe(data => {
			if(data.isSuccess) {
				this.tableData = data.exhibitDatas;
				this.totalCount = data.recordCount;
			}
		})
	}
	//关闭二维码图片
	imgClose(modal){
		modal.hide()
	}
	//车辆二维码下载
	downloadCarCode(modal,data){
		let codeText:any,fileName:any;
		if(data == 'carNumber'){
			if(!this.deviceId){
				this.jsonApiService.message('请选择需要下载二维码的车辆!');
				return;
			}
			this.downloadType = 'car';
			codeText = 'deviceId='+this.deviceId+',carNumber='+this.carNumber;
			fileName = this.carNumber+".jpg";
		}else{
			this.downloadRow = data;
			this.downloadType = 'site';
			codeText = 'stationName='+data.stationName+',areaName='+data.areaName+',stationAddress='+data.stationAddress;
			fileName = data.stationName+".jpg";
		}
		modal.show(0);
		$('#qrcode').empty()
		let qrcode =  new QRCode(document.getElementById("qrcode"), {
			render: "canvas", 
			width: 584,
			height: 584,
			colorDark : "#000000",
			colorLight : "#ffffff",
			correctLevel : QRCode.CorrectLevel.Q
		});
		setTimeout(()=>{
//		    window.pageYOffset = 0;
            document.documentElement.scrollTop = 0;
            document.body.scrollTop = 0;
		    qrcode.makeCode(codeText);
			let ele = $('#qrcodeBox')[0];
			html2canvas(ele).then(canvas => {
                const dataImg = new Image();
                dataImg.src = canvas.toDataURL('image/png');
                $('#qrcodeImg').empty();
                document.querySelector('#qrcodeImg').appendChild(dataImg);
                const alink = document.createElement("a");
                alink.href = dataImg.src;
                alink.download = fileName;
                alink.click();
			});
		},1)
	}
	//站点二维码下载
	downloadCode2(row) {
		let parameters = {
				areaName:row.areaName,
				stationName:row.stationName,
				stationAddress:row.stationAddress,
			};
		let paramsName='params';
		let _url=config.BaseURL + 'QRcodeStationManage/downloadQRcode.html';
		common.exprotDataFile(parameters,paramsName,_url);
	}
	//新增
	add(){
		this.router.navigate(['/langyun/lySystem/siteCode2/siteCode2-page'], {queryParams: {type:'new'}});
	}
	//编辑
	editRow(row){
		this.router.navigate(['/langyun/lySystem/siteCode2/siteCode2-page'], {queryParams: {type:'updata',siteId:row.id,curPage:this.curPage}});
	}
	//删除
	delRow(row){
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(()=>{
			let reqdata = 'id='+row.id;
			this.jsonApiService.http("QRcodeStationManage/deleteQRcodeStationManage.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				this.refresh();
			})
		})
		
	}

	//刷新
	refresh() {
		this.load();
	}
	//查询
	search_data() {
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//模糊搜索
	table_search() {
		this.curPage = 1
		this.search_data();
	}
	//分页回调
	paginate(e) {
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		this.load();
	}
	ngOnDestroy() {
		document.onclick = null;
	}

}